package com.example.chapter6broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    MyReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
        以下部分为动态注册接收器实例
         */
        //实例话广播接收者
        receiver=new MyReceiver();
        //实例化过滤器并设置要过滤的广播
        String action="android.provider.Telephony.SMS_RECEIVED";
        IntentFilter intentFilter=new IntentFilter(action);
        //注册广播
        registerReceiver(receiver,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //当Activity销毁时取消注册BroadcastReceiver
        unregisterReceiver(receiver);
    }
}
